package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ICustomerRelevanceService;
import com.mdd.admin.validate.CustomerRelevanceCreateValidate;
import com.mdd.admin.validate.CustomerRelevanceUpdateValidate;
import com.mdd.admin.validate.CustomerRelevanceSearchValidate;
import com.mdd.admin.vo.CustomerRelevanceListedVo;
import com.mdd.admin.vo.CustomerRelevanceDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.CustomerRelevance;
import com.mdd.common.mapper.CustomerRelevanceMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 客户关联实现类
 * @author LikeAdmin
 */
@Service
public class CustomerRelevanceServiceImpl implements ICustomerRelevanceService {
        
    @Resource
    CustomerRelevanceMapper customerRelevanceMapper;

    /**
     * 客户关联列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<CustomerRelevanceListedVo>
     */
    @Override
    public PageResult<CustomerRelevanceListedVo> list(PageValidate pageValidate, CustomerRelevanceSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<CustomerRelevance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        customerRelevanceMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:customerName@customer_name:str",
        });

        IPage<CustomerRelevance> iPage = customerRelevanceMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<CustomerRelevanceListedVo> list = new LinkedList<>();
        for(CustomerRelevance item : iPage.getRecords()) {
            CustomerRelevanceListedVo vo = new CustomerRelevanceListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 客户关联详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return CustomerRelevance
     */
    @Override
    public CustomerRelevanceDetailVo detail(Integer id) {
        CustomerRelevance model = customerRelevanceMapper.selectOne(
                new QueryWrapper<CustomerRelevance>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        CustomerRelevanceDetailVo vo = new CustomerRelevanceDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 客户关联新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(CustomerRelevanceCreateValidate createValidate) {
        CustomerRelevance model = new CustomerRelevance();
        model.setCustomerName(createValidate.getCustomerName());
        model.setCreatorsName(createValidate.getCreatorsName());
        model.setRemark(createValidate.getRemark());
        customerRelevanceMapper.insert(model);
    }

    /**
     * 客户关联编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(CustomerRelevanceUpdateValidate updateValidate) {
        CustomerRelevance model = customerRelevanceMapper.selectOne(
                new QueryWrapper<CustomerRelevance>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCustomerId(updateValidate.getCustomerId());
        model.setCustomerName(updateValidate.getCustomerName());
        model.setCreatorsId(updateValidate.getCreatorsId());
        model.setCreatorsName(updateValidate.getCreatorsName());
        model.setRemark(updateValidate.getRemark());
        customerRelevanceMapper.updateById(model);
    }

    /**
     * 客户关联删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        CustomerRelevance model = customerRelevanceMapper.selectOne(
                new QueryWrapper<CustomerRelevance>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        customerRelevanceMapper.updateById(model);
    }

}
